package com.itheima.doudizhu;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.TreeSet;

/**
 * @author zhangbaiyan
 * @date 2024-09-13 14:30
 * @description: hash 斗地主练习2
 * @version: 1
 */
public class PokerGame2 {
    //牌盒Map
    //此时我们只要吧牌跟序号对应的光系就可以了 ,不需要按照序号
    static HashMap<Integer, String> hm = new HashMap<>();
    static ArrayList<Integer> list = new ArrayList<>();

    static {
        String[] number = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
        String[] color = {"♦", "♣", "♥", "♠"};

        //序号
        int serialNumber = 1;
        //细节
        for (String n : number) {
            for (String c : color) {
                //依次表示每一个花色

                //依次表示每一个数字
                hm.put(serialNumber, c + n);
                list.add(serialNumber);
                serialNumber++;
            }
        }
        hm.put(serialNumber, "小王");
        list.add(serialNumber);
        serialNumber++;
        hm.put(serialNumber, "大王");
        list.add(serialNumber);

        System.out.println(hm);
        System.out.println("===========================");
        System.out.println(list);
    }

    public PokerGame2() {
        //洗牌
        Collections.shuffle(list);

        //发牌
        TreeSet<Integer> player1 = new TreeSet<>();
        TreeSet<Integer> player2 = new TreeSet<>();
        TreeSet<Integer> player3 = new TreeSet<>();
        TreeSet<Integer> lord = new TreeSet<>();

        for (int i = 0; i < list.size(); i++) {
            //i:依次表示集中的每一个索引
            //list.get(i)元素:牌的序号
            int serialNumber = list.get(i);

            if (i <= 2) {
                lord.add(serialNumber);
                continue;
            }
            if (i % 3 == 0) {
                player1.add(serialNumber);
            } else if (i % 3 == 1) {
                player2.add(serialNumber);
            } else {
                player3.add(serialNumber);
            }

        }

        System.out.println("玩家1:" + player1);
        System.out.println("玩家2:" + player2);
        System.out.println("玩家3:" + player3);
        System.out.println("底牌:" + lord);
        System.out.println("===========================");

        //看牌
        lookPoker("玩家1", player1);
        lookPoker("玩家2", player2);
        lookPoker("玩家3", player3);
        lookPoker("底牌", lord);

    }

    /*
     * 参数一:玩家的名字
     * 参数二:牌的序号
     */
    public void lookPoker(String name, TreeSet<Integer> ts) {
        System.out.print(name + "的牌是:");
        //遍历TreeSet 集合得到每一个序号,再拿着序号到Map集合中去找真正的牌
        for (int i : ts) {
            String poker = hm.get(i);
            System.out.print(poker + " ");
        }
        System.out.println();
    }
}
